home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_emacs-lisp-intro.idb / usr / freeware / info / emacs-lisp-intro.info-2.z / emacs-lisp-intro.info-2
Text File  |  2002-07-08  |  48KB  |  1,105 lines

  1. This is emacs-lisp-intro.info, produced by makeinfo version 4.0b from
  2. emacs-lisp-intro.texi.
  3.  
  4. INFO-DIR-SECTION Emacs
  5. START-INFO-DIR-ENTRY
  6. * Emacs Lisp Intro: (eintr).
  7.               A simple introduction to Emacs Lisp programming.
  8. END-INFO-DIR-ENTRY
  9.  
  10.    This is an introduction to `Programming in Emacs Lisp', for people
  11. who are not programmers.
  12.  
  13.    Edition 2.04, 2001 Dec 17
  14.  
  15.    Copyright (C) 1990, '91, '92, '93, '94, '95, '97, 2001 Free Software
  16. Foundation, Inc.
  17.  
  18.    Permission is granted to copy, distribute and/or modify this document
  19. under the terms of the GNU Free Documentation License, Version 1.1 or
  20. any later version published by the Free Software Foundation; with the
  21. Invariant Section being the Preface, with the Front-Cover Texts being
  22. no Front-Cover Texts, and with the Back-Cover Texts being no Back-Cover
  23. Texts.  A copy of the license is included in the section entitled "GNU
  24. Free Documentation License".
  25.  
  26. 
  27. File: emacs-lisp-intro.info,  Node: Complications,  Next: Byte Compiling,  Prev: Lisp Interpreter,  Up: Lisp Interpreter
  28.  
  29. Complications
  30. -------------
  31.  
  32.    Now, for the first complication.  In addition to lists, the Lisp
  33. interpreter can evaluate a symbol that is not quoted and does not have
  34. parentheses around it.  The Lisp interpreter will attempt to determine
  35. the symbol's value as a "variable".  This situation is described in the
  36. section on variables.  (*Note Variables::.)
  37.  
  38.    The second complication occurs because some functions are unusual
  39. and do not work in the usual manner.  Those that don't are called
  40. "special forms".  They are used for special jobs, like defining a
  41. function, and there are not many of them.  In the next few chapters,
  42. you will be introduced to several of the more important special forms.
  43.  
  44.    The third and final complication is this: if the function that the
  45. Lisp interpreter is looking at is not a special form, and if it is part
  46. of a list, the Lisp interpreter looks to see whether the list has a list
  47. inside of it.  If there is an inner list, the Lisp interpreter first
  48. figures out what it should do with the inside list, and then it works on
  49. the outside list.  If there is yet another list embedded inside the
  50. inner list, it works on that one first, and so on.  It always works on
  51. the innermost list first.  The interpreter works on the innermost list
  52. first, to evaluate the result of that list.  The result may be used by
  53. the enclosing expression.
  54.  
  55.    Otherwise, the interpreter works left to right, from one expression
  56. to the next.
  57.  
  58. 
  59. File: emacs-lisp-intro.info,  Node: Byte Compiling,  Prev: Complications,  Up: Lisp Interpreter
  60.  
  61. Byte Compiling
  62. --------------
  63.  
  64.    One other aspect of interpreting: the Lisp interpreter is able to
  65. interpret two kinds of entity: humanly readable code, on which we will
  66. focus exclusively, and specially processed code, called "byte compiled"
  67. code, which is not humanly readable.  Byte compiled code runs faster
  68. than humanly readable code.
  69.  
  70.    You can transform humanly readable code into byte compiled code by
  71. running one of the compile commands such as `byte-compile-file'.  Byte
  72. compiled code is usually stored in a file that ends with a `.elc'
  73. extension rather than a `.el' extension.  You will see both kinds of
  74. file in the `emacs/lisp' directory; the files to read are those with
  75. `.el' extensions.
  76.  
  77.    As a practical matter, for most things you might do to customize or
  78. extend Emacs, you do not need to byte compile; and I will not discuss
  79. the topic here.  *Note Byte Compilation: (elisp)Byte Compilation, for a
  80. full description of byte compilation.
  81.  
  82. 
  83. File: emacs-lisp-intro.info,  Node: Evaluation,  Next: Variables,  Prev: Lisp Interpreter,  Up: List Processing
  84.  
  85. Evaluation
  86. ==========
  87.  
  88.    When the Lisp interpreter works on an expression, the term for the
  89. activity is called "evaluation".  We say that the interpreter
  90. `evaluates the expression'.  I've used this term several times before.
  91. The word comes from its use in everyday language, `to ascertain the
  92. value or amount of; to appraise', according to `Webster's New
  93. Collegiate Dictionary'.
  94.  
  95.    After evaluating an expression, the Lisp interpreter will most likely
  96. "return" the value that the computer produces by carrying out the
  97. instructions it found in the function definition, or perhaps it will
  98. give up on that function and produce an error message.  (The interpreter
  99. may also find itself tossed, so to speak, to a different function or it
  100. may attempt to repeat continually what it is doing for ever and ever in
  101. what is called an `infinite loop'.  These actions are less common; and
  102. we can ignore them.)  Most frequently, the interpreter returns a value.
  103.  
  104.    At the same time the interpreter returns a value, it may do something
  105. else as well, such as move a cursor or copy a file; this other kind of
  106. action is called a "side effect".  Actions that we humans think are
  107. important, such as printing results, are often "side effects" to the
  108. Lisp interpreter.  The jargon can sound peculiar, but it turns out that
  109. it is fairly easy to learn to use side effects.
  110.  
  111.    In summary, evaluating a symbolic expression most commonly causes the
  112. Lisp interpreter to return a value and perhaps carry out a side effect;
  113. or else produce an error.
  114.  
  115. * Menu:
  116.  
  117. * Evaluating Inner Lists::      Lists within lists...
  118.  
  119. 
  120. File: emacs-lisp-intro.info,  Node: Evaluating Inner Lists,  Prev: Evaluation,  Up: Evaluation
  121.  
  122. Evaluating Inner Lists
  123. ----------------------
  124.  
  125.    If evaluation applies to a list that is inside another list, the
  126. outer list may use the value returned by the first evaluation as
  127. information when the outer list is evaluated.  This explains why inner
  128. expressions are evaluated first: the values they return are used by the
  129. outer expressions.
  130.  
  131.    We can investigate this process by evaluating another addition
  132. example.  Place your cursor after the following expression and type
  133. `C-x C-e':
  134.  
  135.      (+ 2 (+ 3 3))
  136.  
  137. The number 8 will appear in the echo area.
  138.  
  139.    What happens is that the Lisp interpreter first evaluates the inner
  140. expression, `(+ 3 3)', for which the value 6 is returned; then it
  141. evaluates the outer expression as if it were written `(+ 2 6)', which
  142. returns the value 8.  Since there are no more enclosing expressions to
  143. evaluate, the interpreter prints that value in the echo area.
  144.  
  145.    Now it is easy to understand the name of the command invoked by the
  146. keystrokes `C-x C-e': the name is `eval-last-sexp'.  The letters `sexp'
  147. are an abbreviation for `symbolic expression', and `eval' is an
  148. abbreviation for `evaluate'.  The command means `evaluate last symbolic
  149. expression'.
  150.  
  151.    As an experiment, you can try evaluating the expression by putting
  152. the cursor at the beginning of the next line immediately following the
  153. expression, or inside the expression.
  154.  
  155.    Here is another copy of the expression:
  156.  
  157.      (+ 2 (+ 3 3))
  158.  
  159. If you place the cursor at the beginning of the blank line that
  160. immediately follows the expression and type `C-x C-e', you will still
  161. get the value 8 printed in the echo area.  Now try putting the cursor
  162. inside the expression.  If you put it right after the next to last
  163. parenthesis (so it appears to sit on top of the last parenthesis), you
  164. will get a 6 printed in the echo area!  This is because the command
  165. evaluates the expression `(+ 3 3)'.
  166.  
  167.    Now put the cursor immediately after a number.  Type `C-x C-e' and
  168. you will get the number itself.  In Lisp, if you evaluate a number, you
  169. get the number itself--this is how numbers differ from symbols.  If you
  170. evaluate a list starting with a symbol like `+', you will get a value
  171. returned that is the result of the computer carrying out the
  172. instructions in the function definition attached to that name.  If a
  173. symbol by itself is evaluated, something different happens, as we will
  174. see in the next section.
  175.  
  176. 
  177. File: emacs-lisp-intro.info,  Node: Variables,  Next: Arguments,  Prev: Evaluation,  Up: List Processing
  178.  
  179. Variables
  180. =========
  181.  
  182.    In Emacs Lisp, a symbol can have a value attached to it just as it
  183. can have a function definition attached to it.  The two are different.
  184. The function definition is a set of instructions that a computer will
  185. obey.  A value, on the other hand, is something, such as number or a
  186. name, that can vary (which is why such a symbol is called a variable).
  187. The value of a symbol can be any expression in Lisp, such as a symbol,
  188. number, list, or string.  A symbol that has a value is often called a
  189. "variable".
  190.  
  191.    A symbol can have both a function definition and a value attached to
  192. it at the same time.  Or it can have just one or the other.  The two
  193. are separate.  This is somewhat similar to the way the name Cambridge
  194. can refer to the city in Massachusetts and have some information
  195. attached to the name as well, such as "great programming center".
  196.  
  197.    Another way to think about this is to imagine a symbol as being a
  198. chest of drawers.  The function definition is put in one drawer, the
  199. value in another, and so on.  What is put in the drawer holding the
  200. value can be changed without affecting the contents of the drawer
  201. holding the function definition, and vice-versa.
  202.  
  203. * Menu:
  204.  
  205. * fill-column Example::
  206. * Void Function::               The error message for a symbol
  207.                                   without a function.
  208. * Void Variable::               The error message for a symbol without a value.
  209.  
  210. 
  211. File: emacs-lisp-intro.info,  Node: fill-column Example,  Next: Void Function,  Prev: Variables,  Up: Variables
  212.  
  213. `fill-column', an Example Variable
  214. ----------------------------------
  215.  
  216.    The variable `fill-column' illustrates a symbol with a value
  217. attached to it: in every GNU Emacs buffer, this symbol is set to some
  218. value, usually 72 or 70, but sometimes to some other value.  To find the
  219. value of this symbol, evaluate it by itself.  If you are reading this in
  220. Info inside of GNU Emacs, you can do this by putting the cursor after
  221. the symbol and typing `C-x C-e':
  222.  
  223.      fill-column
  224.  
  225. After I typed `C-x C-e', Emacs printed the number 72 in my echo area.
  226. This is the value for which `fill-column' is set for me as I write
  227. this.  It may be different for you in your Info buffer.  Notice that
  228. the value returned as a variable is printed in exactly the same way as
  229. the value returned by a function carrying out its instructions.  From
  230. the point of view of the Lisp interpreter, a value returned is a value
  231. returned.  What kind of expression it came from ceases to matter once
  232. the value is known.
  233.  
  234.    A symbol can have any value attached to it or, to use the jargon, we
  235. can "bind" the variable to a value: to a number, such as 72; to a
  236. string, `"such as this"'; to a list, such as `(spruce pine oak)'; we
  237. can even bind a variable to a function definition.
  238.  
  239.    A symbol can be bound to a value in several ways.  *Note Setting the
  240. Value of a Variable: set & setq, for information about one way to do
  241. this.
  242.  
  243. 
  244. File: emacs-lisp-intro.info,  Node: Void Function,  Next: Void Variable,  Prev: fill-column Example,  Up: Variables
  245.  
  246. Error Message for a Symbol Without a Function
  247. ---------------------------------------------
  248.  
  249.    When we evaluated `fill-column' to find its value as a variable, we
  250. did not place parentheses around the word.  This is because we did not
  251. intend to use it as a function name.
  252.  
  253.    If `fill-column' were the first or only element of a list, the Lisp
  254. interpreter would attempt to find the function definition attached to
  255. it.  But `fill-column' has no function definition.  Try evaluating this:
  256.  
  257.      (fill-column)
  258.  
  259. In GNU Emacs version 21, you will create a `*Backtrace*' buffer that
  260. says:
  261.  
  262.      ---------- Buffer: *Backtrace* ----------
  263.      Debugger entered--Lisp error: (void-function fill-column)
  264.        (fill-column)
  265.        eval((fill-column))
  266.        eval-last-sexp-1(nil)
  267.        eval-last-sexp(nil)
  268.        call-interactively(eval-last-sexp)
  269.      ---------- Buffer: *Backtrace* ----------
  270.  
  271. (Remember, to quit the debugger and make the debugger window go away,
  272. type `q' in the `*Backtrace*' buffer.)
  273.  
  274.    In GNU Emacs 20 and before, you will produce an error message that
  275. says:
  276.  
  277.      Symbol's function definition is void: fill-column
  278.  
  279. (The message will go away away as soon as you move the cursor or type
  280. another key.)
  281.  
  282. 
  283. File: emacs-lisp-intro.info,  Node: Void Variable,  Prev: Void Function,  Up: Variables
  284.  
  285. Error Message for a Symbol Without a Value
  286. ------------------------------------------
  287.  
  288.    If you attempt to evaluate a symbol that does not have a value bound
  289. to it, you will receive an error message.  You can see this by
  290. experimenting with our 2 plus 2 addition.  In the following expression,
  291. put your cursor right after the `+', before the first number 2, type
  292. `C-x C-e':
  293.  
  294.      (+ 2 2)
  295.  
  296. In GNU Emacs 21, you will create a `*Backtrace*' buffer that says:
  297.  
  298.      ---------- Buffer: *Backtrace* ----------
  299.      Debugger entered--Lisp error: (void-variable +)
  300.        eval(+)
  301.        eval-last-sexp-1(nil)
  302.        eval-last-sexp(nil)
  303.        call-interactively(eval-last-sexp)
  304.      ---------- Buffer: *Backtrace* ----------
  305.  
  306. (As with the other times we entered the debugger, you can quit by
  307. typing `q' in the `*Backtrace*' buffer.)
  308.  
  309.    This backtrace is different from the very first error message we saw,
  310. which said, `Debugger entered--Lisp error: (void-function this)'.  In
  311. this case, the function does not have a value as a variable; while in
  312. the other error message, the function (the word `this') did not have a
  313. definition.
  314.  
  315.    In this experiment with the `+', what we did was cause the Lisp
  316. interpreter to evaluate the `+' and look for the value of the variable
  317. instead of the function definition.  We did this by placing the cursor
  318. right after the symbol rather than after the parenthesis of the
  319. enclosing list as we did before.  As a consequence, the Lisp interpreter
  320. evaluated the preceding s-expression, which in this case was the `+' by
  321. itself.
  322.  
  323.    Since `+' does not have a value bound to it, just the function
  324. definition, the error message reported that the symbol's value as a
  325. variable was void.
  326.  
  327.    In GNU Emacs version 20 and before, your error message will say:
  328.  
  329.      Symbol's value as variable is void: +
  330.  
  331. The meaning is the same as in GNU Emacs 21.
  332.  
  333. 
  334. File: emacs-lisp-intro.info,  Node: Arguments,  Next: set & setq,  Prev: Variables,  Up: List Processing
  335.  
  336. Arguments
  337. =========
  338.  
  339.    To see how information is passed to functions, let's look again at
  340. our old standby, the addition of two plus two.  In Lisp, this is written
  341. as follows:
  342.  
  343.      (+ 2 2)
  344.  
  345.    If you evaluate this expression, the number 4 will appear in your
  346. echo area.  What the Lisp interpreter does is add the numbers that
  347. follow the `+'.
  348.  
  349.    The numbers added by `+' are called the "arguments" of the function
  350. `+'.  These numbers are the information that is given to or "passed" to
  351. the function.
  352.  
  353.    The word `argument' comes from the way it is used in mathematics and
  354. does not refer to a disputation between two people; instead it refers to
  355. the information presented to the function, in this case, to the `+'.
  356. In Lisp, the arguments to a function are the atoms or lists that follow
  357. the function.  The values returned by the evaluation of these atoms or
  358. lists are passed to the function.  Different functions require
  359. different numbers of arguments; some functions require none at all.(1)
  360.  
  361. * Menu:
  362.  
  363. * Data types::                  Types of data passed to a function.
  364. * Args as Variable or List::    An argument can be the value
  365.                                   of a variable or list.
  366. * Variable Number of Arguments::  Some functions may take a
  367.                                   variable number of arguments.
  368. * Wrong Type of Argument::      Passing an argument of the wrong type
  369.                                   to a function.
  370. * message::                     A useful function for sending messages.
  371.  
  372.    ---------- Footnotes ----------
  373.  
  374.    (1) It is curious to track the path by which the word `argument'
  375. came to have two different meanings, one in mathematics and the other in
  376. everyday English.  According to the `Oxford English Dictionary', the
  377. word derives from the Latin for `to make clear, prove'; thus it came to
  378. mean, by one thread of derivation, `the evidence offered as proof',
  379. which is to say, `the information offered', which led to its meaning in
  380. Lisp.  But in the other thread of derivation, it came to mean `to
  381. assert in a manner against which others may make counter assertions',
  382. which led to the meaning of the word as a disputation.  (Note here that
  383. the English word has two different definitions attached to it at the
  384. same time.  By contrast, in Emacs Lisp, a symbol cannot have two
  385. different function definitions at the same time.)
  386.  
  387. 
  388. File: emacs-lisp-intro.info,  Node: Data types,  Next: Args as Variable or List,  Prev: Arguments,  Up: Arguments
  389.  
  390. Arguments' Data Types
  391. ---------------------
  392.  
  393.    The type of data that should be passed to a function depends on what
  394. kind of information it uses.  The arguments to a function such as `+'
  395. must have values that are numbers, since `+' adds numbers.  Other
  396. functions use different kinds of data for their arguments.
  397.  
  398.    For example, the `concat' function links together or unites two or
  399. more strings of text to produce a string.  The arguments are strings.
  400. Concatenating the two character strings `abc', `def' produces the
  401. single string `abcdef'.  This can be seen by evaluating the following:
  402.  
  403.      (concat "abc" "def")
  404.  
  405. The value produced by evaluating this expression is `"abcdef"'.
  406.  
  407.    A function such as `substring' uses both a string and numbers as
  408. arguments.  The function returns a part of the string, a substring of
  409. the first argument.  This function takes three arguments.  Its first
  410. argument is the string of characters, the second and third arguments are
  411. numbers that indicate the beginning and end of the substring.  The
  412. numbers are a count of the number of characters (including spaces and
  413. punctuations) from the beginning of the string.
  414.  
  415.    For example, if you evaluate the following:
  416.  
  417.      (substring "The quick brown fox jumped." 16 19)
  418.  
  419. you will see `"fox"' appear in the echo area.  The arguments are the
  420. string and the two numbers.
  421.  
  422.    Note that the string passed to `substring' is a single atom even
  423. though it is made up of several words separated by spaces.  Lisp counts
  424. everything between the two quotation marks as part of the string,
  425. including the spaces.  You can think of the `substring' function as a
  426. kind of `atom smasher' since it takes an otherwise indivisible atom and
  427. extracts a part.  However, `substring' is only able to extract a
  428. substring from an argument that is a string, not from another type of
  429. atom such as a number or symbol.
  430.  
  431. 
  432. File: emacs-lisp-intro.info,  Node: Args as Variable or List,  Next: Variable Number of Arguments,  Prev: Data types,  Up: Arguments
  433.  
  434. An Argument as the Value of a Variable or List
  435. ----------------------------------------------
  436.  
  437.    An argument can be a symbol that returns a value when it is
  438. evaluated.  For example, when the symbol `fill-column' by itself is
  439. evaluated, it returns a number.  This number can be used in an addition.
  440.  
  441.    Position the cursor after the following expression and type `C-x
  442. C-e':
  443.  
  444.      (+ 2 fill-column)
  445.  
  446. The value will be a number two more than what you get by evaluating
  447. `fill-column' alone.  For me, this is 74, because the value of
  448. `fill-column' is 72.
  449.  
  450.    As we have just seen, an argument can be a symbol that returns a
  451. value when evaluated.  In addition, an argument can be a list that
  452. returns a value when it is evaluated.  For example, in the following
  453. expression, the arguments to the function `concat' are the strings
  454. `"The "' and `" red foxes."' and the list `(number-to-string (+ 2
  455. fill-column))'.
  456.  
  457.      (concat "The " (number-to-string (+ 2 fill-column)) " red foxes.")
  458.  
  459. If you evaluate this expression--and if, as with my Emacs,
  460. `fill-column' evaluates to 72--`"The 74 red foxes."' will appear in the
  461. echo area.  (Note that you must put spaces after the word `The' and
  462. before the word `red' so they will appear in the final string.  The
  463. function `number-to-string' converts the integer that the addition
  464. function returns to a string.  `number-to-string' is also known as
  465. `int-to-string'.)
  466.  
  467. 
  468. File: emacs-lisp-intro.info,  Node: Variable Number of Arguments,  Next: Wrong Type of Argument,  Prev: Args as Variable or List,  Up: Arguments
  469.  
  470. Variable Number of Arguments
  471. ----------------------------
  472.  
  473.    Some functions, such as `concat', `+' or `*', take any number of
  474. arguments.  (The `*' is the symbol for multiplication.)  This can be
  475. seen by evaluating each of the following expressions in the usual way.
  476. What you will see in the echo area is printed in this text after `=>',
  477. which you may read as `evaluates to'.
  478.  
  479.    In the first set, the functions have no arguments:
  480.  
  481.      (+)       => 0
  482.      
  483.      (*)       => 1
  484.  
  485.    In this set, the functions have one argument each:
  486.  
  487.      (+ 3)     => 3
  488.      
  489.      (* 3)     => 3
  490.  
  491.    In this set, the functions have three arguments each:
  492.  
  493.      (+ 3 4 5) => 12
  494.      
  495.      (* 3 4 5) => 60
  496.  
  497. 
  498. File: emacs-lisp-intro.info,  Node: Wrong Type of Argument,  Next: message,  Prev: Variable Number of Arguments,  Up: Arguments
  499.  
  500. Using the Wrong Type Object as an Argument
  501. ------------------------------------------
  502.  
  503.    When a function is passed an argument of the wrong type, the Lisp
  504. interpreter produces an error message.  For example, the `+' function
  505. expects the values of its arguments to be numbers.  As an experiment we
  506. can pass it the quoted symbol `hello' instead of a number.  Position
  507. the cursor after the following expression and type `C-x C-e':
  508.  
  509.      (+ 2 'hello)
  510.  
  511. When you do this you will generate an error message.  What has happened
  512. is that `+' has tried to add the 2 to the value returned by `'hello',
  513. but the value returned by `'hello' is the symbol `hello', not a number.
  514. Only numbers can be added.  So `+' could not carry out its addition.
  515.  
  516.    In GNU Emacs version 21, you will create and enter a `*Backtrace*'
  517. buffer that says:
  518.  
  519.      ---------- Buffer: *Backtrace* ----------
  520.      Debugger entered--Lisp error:
  521.               (wrong-type-argument number-or-marker-p hello)
  522.        +(2 hello)
  523.        eval((+ 2 (quote hello)))
  524.        eval-last-sexp-1(nil)
  525.        eval-last-sexp(nil)
  526.        call-interactively(eval-last-sexp)
  527.      ---------- Buffer: *Backtrace* ----------
  528.  
  529. As usual, the error message tries to be helpful and makes sense after
  530. you learn how to read it.
  531.  
  532.    The first part of the error message is straightforward; it says
  533. `wrong type argument'.  Next comes the mysterious jargon word
  534. `number-or-marker-p'.  This word is trying to tell you what kind of
  535. argument the `+' expected.
  536.  
  537.    The symbol `number-or-marker-p' says that the Lisp interpreter is
  538. trying to determine whether the information presented it (the value of
  539. the argument) is a number or a marker (a special object representing a
  540. buffer position).  What it does is test to see whether the `+' is being
  541. given numbers to add.  It also tests to see whether the argument is
  542. something called a marker, which is a specific feature of Emacs Lisp.
  543. (In Emacs, locations in a buffer are recorded as markers.  When the
  544. mark is set with the `C-@' or `C-<SPC>' command, its position is kept
  545. as a marker.  The mark can be considered a number--the number of
  546. characters the location is from the beginning of the buffer.)  In Emacs
  547. Lisp, `+' can be used to add the numeric value of marker positions as
  548. numbers.
  549.  
  550.    The `p' of `number-or-marker-p' is the embodiment of a practice
  551. started in the early days of Lisp programming.  The `p' stands for
  552. `predicate'.  In the jargon used by the early Lisp researchers, a
  553. predicate refers to a function to determine whether some property is
  554. true or false.  So the `p' tells us that `number-or-marker-p' is the
  555. name of a function that determines whether it is true or false that the
  556. argument supplied is a number or a marker.  Other Lisp symbols that end
  557. in `p' include `zerop', a function that tests whether its argument has
  558. the value of zero, and `listp', a function that tests whether its
  559. argument is a list.
  560.  
  561.    Finally, the last part of the error message is the symbol `hello'.
  562. This is the value of the argument that was passed to `+'.  If the
  563. addition had been passed the correct type of object, the value passed
  564. would have been a number, such as 37, rather than a symbol like
  565. `hello'.  But then you would not have got the error message.
  566.  
  567.    In GNU Emacs version 20 and before, the echo area displays an error
  568. message that says:
  569.  
  570.      Wrong type argument: number-or-marker-p, hello
  571.  
  572.    This says, in different words, the same as the top line of the
  573. `*Backtrace*' buffer.
  574.  
  575. 
  576. File: emacs-lisp-intro.info,  Node: message,  Prev: Wrong Type of Argument,  Up: Arguments
  577.  
  578. The `message' Function
  579. ----------------------
  580.  
  581.    Like `+', the `message' function takes a variable number of
  582. arguments.  It is used to send messages to the user and is so useful
  583. that we will describe it here.
  584.  
  585.    A message is printed in the echo area.  For example, you can print a
  586. message in your echo area by evaluating the following list:
  587.  
  588.      (message "This message appears in the echo area!")
  589.  
  590.    The whole string between double quotation marks is a single argument
  591. and is printed in toto.  (Note that in this example, the message itself
  592. will appear in the echo area within double quotes; that is because you
  593. see the value returned by the `message' function.  In most uses of
  594. `message' in programs that you write, the text will be printed in the
  595. echo area as a side-effect, without the quotes.  *Note
  596. `multiply-by-seven' in detail: multiply-by-seven in detail, for an
  597. example of this.)
  598.  
  599.    However, if there is a `%s' in the quoted string of characters, the
  600. `message' function does not print the `%s' as such, but looks to the
  601. argument that follows the string.  It evaluates the second argument and
  602. prints the value at the location in the string where the `%s' is.
  603.  
  604.    You can see this by positioning the cursor after the following
  605. expression and typing `C-x C-e':
  606.  
  607.      (message "The name of this buffer is: %s." (buffer-name))
  608.  
  609. In Info, `"The name of this buffer is: *info*."' will appear in the
  610. echo area.  The function `buffer-name' returns the name of the buffer
  611. as a string, which the `message' function inserts in place of `%s'.
  612.  
  613.    To print a value as an integer, use `%d' in the same way as `%s'.
  614. For example, to print a message in the echo area that states the value
  615. of the `fill-column', evaluate the following:
  616.  
  617.      (message "The value of fill-column is %d." fill-column)
  618.  
  619. On my system, when I evaluate this list, `"The value of fill-column is
  620. 72."' appears in my echo area(1).
  621.  
  622.    If there is more than one `%s' in the quoted string, the value of
  623. the first argument following the quoted string is printed at the
  624. location of the first `%s' and the value of the second argument is
  625. printed at the location of the second `%s', and so on.
  626.  
  627.    For example, if you evaluate the following,
  628.  
  629.      (message "There are %d %s in the office!"
  630.               (- fill-column 14) "pink elephants")
  631.  
  632. a rather whimsical message will appear in your echo area.  On my system
  633. it says, `"There are 58 pink elephants in the office!"'.
  634.  
  635.    The expression `(- fill-column 14)' is evaluated and the resulting
  636. number is inserted in place of the `%d'; and the string in double
  637. quotes, `"pink elephants"', is treated as a single argument and
  638. inserted in place of the `%s'.  (That is to say, a string between
  639. double quotes evaluates to itself, like a number.)
  640.  
  641.    Finally, here is a somewhat complex example that not only illustrates
  642. the computation of a number, but also shows how you can use an
  643. expression within an expression to generate the text that is substituted
  644. for `%s':
  645.  
  646.      (message "He saw %d %s"
  647.               (- fill-column 34)
  648.               (concat "red "
  649.                       (substring
  650.                        "The quick brown foxes jumped." 16 21)
  651.                       " leaping."))
  652.  
  653.    In this example, `message' has three arguments: the string, `"He saw
  654. %d %s"', the expression, `(- fill-column 32)', and the expression
  655. beginning with the function `concat'.  The value resulting from the
  656. evaluation of `(- fill-column 32)' is inserted in place of the `%d';
  657. and the value returned by the expression beginning with `concat' is
  658. inserted in place of the `%s'.
  659.  
  660.    When I evaluate the expression, the message `"He saw 38 red foxes
  661. leaping."' appears in my echo area.
  662.  
  663.    ---------- Footnotes ----------
  664.  
  665.    (1) Actually, you can use `%s' to print a number.  It is
  666. non-specific.  `%d' prints only the part of a number left of a decimal
  667. point, and not anything that is not a number.
  668.  
  669. 
  670. File: emacs-lisp-intro.info,  Node: set & setq,  Next: Summary,  Prev: Arguments,  Up: List Processing
  671.  
  672. Setting the Value of a Variable
  673. ===============================
  674.  
  675.    There are several ways by which a variable can be given a value.
  676. One of the ways is to use either the function `set' or the function
  677. `setq'.  Another way is to use `let' (*note let::).  (The jargon for
  678. this process is to "bind" a variable to a value.)
  679.  
  680.    The following sections not only describe how `set' and `setq' work
  681. but also illustrate how arguments are passed.
  682.  
  683. * Menu:
  684.  
  685. * Using set::                   Setting values.
  686. * Using setq::                  Setting a quoted value.
  687. * Counting::                    Using `setq' to count.
  688.  
  689. 
  690. File: emacs-lisp-intro.info,  Node: Using set,  Next: Using setq,  Prev: set & setq,  Up: set & setq
  691.  
  692. Using `set'
  693. -----------
  694.  
  695.    To set the value of the symbol `flowers' to the list `'(rose violet
  696. daisy buttercup)', evaluate the following expression by positioning the
  697. cursor after the expression and typing `C-x C-e'.
  698.  
  699.      (set 'flowers '(rose violet daisy buttercup))
  700.  
  701. The list `(rose violet daisy buttercup)' will appear in the echo area.
  702. This is what is _returned_ by the `set' function.  As a side effect,
  703. the symbol `flowers' is bound to the list ; that is, the symbol
  704. `flowers', which can be viewed as a variable, is given the list as its
  705. value.  (This process, by the way, illustrates how a side effect to the
  706. Lisp interpreter, setting the value, can be the primary effect that we
  707. humans are interested in.  This is because every Lisp function must
  708. return a value if it does not get an error, but it will only have a
  709. side effect if it is designed to have one.)
  710.  
  711.    After evaluating the `set' expression, you can evaluate the symbol
  712. `flowers' and it will return the value you just set.  Here is the
  713. symbol.  Place your cursor after it and type `C-x C-e'.
  714.  
  715.      flowers
  716.  
  717. When you evaluate `flowers', the list `(rose violet daisy buttercup)'
  718. appears in the echo area.
  719.  
  720.    Incidentally, if you evaluate `'flowers', the variable with a quote
  721. in front of it, what you will see in the echo area is the symbol itself,
  722. `flowers'.  Here is the quoted symbol, so you can try this:
  723.  
  724.      'flowers
  725.  
  726.    Note also, that when you use `set', you need to quote both arguments
  727. to `set', unless you want them evaluated.  Since we do not want either
  728. argument evaluated, neither the variable `flowers' nor the list `(rose
  729. violet daisy buttercup)', both are quoted.  (When you use `set' without
  730. quoting its first argument, the first argument is evaluated before
  731. anything else is done.  If you did this and `flowers' did not have a
  732. value already, you would get an error message that the `Symbol's value
  733. as variable is void'; on the other hand, if `flowers' did return a
  734. value after it was evaluated, the `set' would attempt to set the value
  735. that was returned.  There are situations where this is the right thing
  736. for the function to do; but such situations are rare.)
  737.  
  738. 
  739. File: emacs-lisp-intro.info,  Node: Using setq,  Next: Counting,  Prev: Using set,  Up: set & setq
  740.  
  741. Using `setq'
  742. ------------
  743.  
  744.    As a practical matter, you almost always quote the first argument to
  745. `set'.  The combination of `set' and a quoted first argument is so
  746. common that it has its own name: the special form `setq'.  This special
  747. form is just like `set' except that the first argument is quoted
  748. automatically, so you don't need to type the quote mark yourself.
  749. Also, as an added convenience, `setq' permits you to set several
  750. different variables to different values, all in one expression.
  751.  
  752.    To set the value of the variable `carnivores' to the list `'(lion
  753. tiger leopard)' using `setq', the following expression is used:
  754.  
  755.      (setq carnivores '(lion tiger leopard))
  756.  
  757. This is exactly the same as using `set' except the first argument is
  758. automatically quoted by `setq'.  (The `q' in `setq' means `quote'.)
  759.  
  760.    With `set', the expression would look like this:
  761.  
  762.      (set 'carnivores '(lion tiger leopard))
  763.  
  764.    Also, `setq' can be used to assign different values to different
  765. variables.  The first argument is bound to the value of the second
  766. argument, the third argument is bound to the value of the fourth
  767. argument, and so on.  For example, you could use the following to
  768. assign a list of trees to the symbol `trees' and a list of herbivores
  769. to the symbol `herbivores':
  770.  
  771.      (setq trees '(pine fir oak maple)
  772.            herbivores '(gazelle antelope zebra))
  773.  
  774. (The expression could just as well have been on one line, but it might
  775. not have fit on a page; and humans find it easier to read nicely
  776. formatted lists.)
  777.  
  778.    Although I have been using the term `assign', there is another way of
  779. thinking about the workings of `set' and `setq'; and that is to say
  780. that `set' and `setq' make the symbol _point_ to the list.  This latter
  781. way of thinking is very common and in forthcoming chapters we shall
  782. come upon at least one symbol that has `pointer' as part of its name.
  783. The name is chosen because the symbol has a value, specifically a list,
  784. attached to it; or, expressed another way, the symbol is set to "point"
  785. to the list.
  786.  
  787. 
  788. File: emacs-lisp-intro.info,  Node: Counting,  Prev: Using setq,  Up: set & setq
  789.  
  790. Counting
  791. --------
  792.  
  793.    Here is an example that shows how to use `setq' in a counter.  You
  794. might use this to count how many times a part of your program repeats
  795. itself.  First set a variable to zero; then add one to the number each
  796. time the program repeats itself.  To do this, you need a variable that
  797. serves as a counter, and two expressions: an initial `setq' expression
  798. that sets the counter variable to zero; and a second `setq' expression
  799. that increments the counter each time it is evaluated.
  800.  
  801.      (setq counter 0)                ; Let's call this the initializer.
  802.      
  803.      (setq counter (+ counter 1))    ; This is the incrementer.
  804.      
  805.      counter                         ; This is the counter.
  806.  
  807. (The text following the `;' are comments.  *Note Change a Function
  808. Definition: Change a defun.)
  809.  
  810.    If you evaluate the first of these expressions, the initializer,
  811. `(setq counter 0)', and then evaluate the third expression, `counter',
  812. the number `0' will appear in the echo area.  If you then evaluate the
  813. second expression, the incrementer, `(setq counter (+ counter 1))', the
  814. counter will get the value 1.  So if you again evaluate `counter', the
  815. number `1' will appear in the echo area.  Each time you evaluate the
  816. second expression, the value of the counter will be incremented.
  817.  
  818.    When you evaluate the incrementer, `(setq counter (+ counter 1))',
  819. the Lisp interpreter first evaluates the innermost list; this is the
  820. addition.  In order to evaluate this list, it must evaluate the variable
  821. `counter' and the number `1'.  When it evaluates the variable
  822. `counter', it receives its current value.  It passes this value and the
  823. number `1' to the `+' which adds them together.  The sum is then
  824. returned as the value of the inner list and passed to the `setq' which
  825. sets the variable `counter' to this new value.  Thus, the value of the
  826. variable, `counter', is changed.
  827.  
  828. 
  829. File: emacs-lisp-intro.info,  Node: Summary,  Next: Error Message Exercises,  Prev: set & setq,  Up: List Processing
  830.  
  831. Summary
  832. =======
  833.  
  834.    Learning Lisp is like climbing a hill in which the first part is the
  835. steepest.  You have now climbed the most difficult part; what remains
  836. becomes easier as you progress onwards.
  837.  
  838.    In summary,
  839.  
  840.    * Lisp programs are made up of expressions, which are lists or
  841.      single atoms.
  842.  
  843.    * Lists are made up of zero or more atoms or inner lists, separated
  844.      by whitespace and surrounded by parentheses.  A list can be empty.
  845.  
  846.    * Atoms are multi-character symbols, like `forward-paragraph', single
  847.      character symbols like `+', strings of characters between double
  848.      quotation marks, or numbers.
  849.  
  850.    * A number evaluates to itself.
  851.  
  852.    * A string between double quotes also evaluates to itself.
  853.  
  854.    * When you evaluate a symbol by itself, its value is returned.
  855.  
  856.    * When you evaluate a list, the Lisp interpreter looks at the first
  857.      symbol in the list and then at the function definition bound to
  858.      that symbol.  Then the instructions in the function definition are
  859.      carried out.
  860.  
  861.    * A single-quote, `'', tells the Lisp interpreter that it should
  862.      return the following expression as written, and not evaluate it as
  863.      it would if the quote were not there.
  864.  
  865.    * Arguments are the information passed to a function.  The arguments
  866.      to a function are computed by evaluating the rest of the elements
  867.      of the list of which the function is the first element.
  868.  
  869.    * A function always returns a value when it is evaluated (unless it
  870.      gets an error); in addition, it may also carry out some action
  871.      called a "side effect".  In many cases, a function's primary
  872.      purpose is to create a side effect.
  873.  
  874. 
  875. File: emacs-lisp-intro.info,  Node: Error Message Exercises,  Prev: Summary,  Up: List Processing
  876.  
  877. Exercises
  878. =========
  879.  
  880.    A few simple exercises:
  881.  
  882.    * Generate an error message by evaluating an appropriate symbol that
  883.      is not within parentheses.
  884.  
  885.    * Generate an error message by evaluating an appropriate symbol that
  886.      is between parentheses.
  887.  
  888.    * Create a counter that increments by two rather than one.
  889.  
  890.    * Write an expression that prints a message in the echo area when
  891.      evaluated.
  892.  
  893. 
  894. File: emacs-lisp-intro.info,  Node: Practicing Evaluation,  Next: Writing Defuns,  Prev: List Processing,  Up: Top
  895.  
  896. Practicing Evaluation
  897. *********************
  898.  
  899.    Before learning how to write a function definition in Emacs Lisp, it
  900. is useful to spend a little time evaluating various expressions that
  901. have already been written.  These expressions will be lists with the
  902. functions as their first (and often only) element.  Since some of the
  903. functions associated with buffers are both simple and interesting, we
  904. will start with those.  In this section, we will evaluate a few of
  905. these.  In another section, we will study the code of several other
  906. buffer-related functions, to see how they were written.
  907.  
  908. * Menu:
  909.  
  910. * How to Evaluate::             Typing editing commands or C-x C-e
  911.                                   causes evaluation.
  912. * Buffer Names::                Buffers and files are different.
  913. * Getting Buffers::             Getting a buffer itself, not merely its name.
  914. * Switching Buffers::           How to change to another buffer.
  915. * Buffer Size & Locations::     Where point is located and the size of
  916.                                 the buffer.
  917. * Evaluation Exercise::
  918.  
  919. 
  920. File: emacs-lisp-intro.info,  Node: How to Evaluate,  Next: Buffer Names,  Prev: Practicing Evaluation,  Up: Practicing Evaluation
  921.  
  922. How to Evaluate
  923. ===============
  924.  
  925.    Whenever you give an editing command to Emacs Lisp, such as the
  926. command to move the cursor or to scroll the screen, you are evaluating
  927. an expression, the first element of which is a function.  This is how
  928. Emacs works.
  929.  
  930.    When you type keys, you cause the Lisp interpreter to evaluate an
  931. expression and that is how you get your results.  Even typing plain text
  932. involves evaluating an Emacs Lisp function, in this case, one that uses
  933. `self-insert-command', which simply inserts the character you typed.
  934. The functions you evaluate by typing keystrokes are called
  935. "interactive" functions, or "commands"; how you make a function
  936. interactive will be illustrated in the chapter on how to write function
  937. definitions.  *Note Making a Function Interactive: Interactive.
  938.  
  939.    In addition to typing keyboard commands, we have seen a second way to
  940. evaluate an expression: by positioning the cursor after a list and
  941. typing `C-x C-e'.  This is what we will do in the rest of this section.
  942. There are other ways to evaluate an expression as well; these will be
  943. described as we come to them.
  944.  
  945.    Besides being used for practicing evaluation, the functions shown in
  946. the next few sections are important in their own right.  A study of
  947. these functions makes clear the distinction between buffers and files,
  948. how to switch to a buffer, and how to determine a location within it.
  949.  
  950. 
  951. File: emacs-lisp-intro.info,  Node: Buffer Names,  Next: Getting Buffers,  Prev: How to Evaluate,  Up: Practicing Evaluation
  952.  
  953. Buffer Names
  954. ============
  955.  
  956.    The two functions, `buffer-name' and `buffer-file-name', show the
  957. difference between a file and a buffer.  When you evaluate the
  958. following expression, `(buffer-name)', the name of the buffer appears
  959. in the echo area.  When you evaluate `(buffer-file-name)', the name of
  960. the file to which the buffer refers appears in the echo area.  Usually,
  961. the name returned by `(buffer-name)' is the same as the name of the
  962. file to which it refers, and the name returned by `(buffer-file-name)'
  963. is the full path-name of the file.
  964.  
  965.    A file and a buffer are two different entities.  A file is
  966. information recorded permanently in the computer (unless you delete
  967. it).  A buffer, on the other hand, is information inside of Emacs that
  968. will vanish at the end of the editing session (or when you kill the
  969. buffer).  Usually, a buffer contains information that you have copied
  970. from a file; we say the buffer is "visiting" that file.  This copy is
  971. what you work on and modify.  Changes to the buffer do not change the
  972. file, until you save the buffer.  When you save the buffer, the buffer
  973. is copied to the file and is thus saved permanently.
  974.  
  975.    If you are reading this in Info inside of GNU Emacs, you can evaluate
  976. each of the following expressions by positioning the cursor after it and
  977. typing `C-x C-e'.
  978.  
  979.      (buffer-name)
  980.      
  981.      (buffer-file-name)
  982.  
  983. When I do this, `"introduction.texinfo"' is the value returned by
  984. evaluating `(buffer-name)', and
  985. `"/gnu/work/intro/introduction.texinfo"' is the value returned by
  986. evaluating `(buffer-file-name)'.  The former is the name of the buffer
  987. and the latter is the name of the file.  (In the expressions, the
  988. parentheses tell the Lisp interpreter to treat `buffer-name' and
  989. `buffer-file-name' as functions; without the parentheses, the
  990. interpreter would attempt to evaluate the symbols as variables.  *Note
  991. Variables::.)
  992.  
  993.    In spite of the distinction between files and buffers, you will often
  994. find that people refer to a file when they mean a buffer and vice-versa.
  995. Indeed, most people say, "I am editing a file," rather than saying, "I
  996. am editing a buffer which I will soon save to a file."  It is almost
  997. always clear from context what people mean.  When dealing with computer
  998. programs, however, it is important to keep the distinction in mind,
  999. since the computer is not as smart as a person.
  1000.  
  1001.    The word `buffer', by the way, comes from the meaning of the word as
  1002. a cushion that deadens the force of a collision.  In early computers, a
  1003. buffer cushioned the interaction between files and the computer's
  1004. central processing unit.  The drums or tapes that held a file and the
  1005. central processing unit were pieces of equipment that were very
  1006. different from each other, working at their own speeds, in spurts.  The
  1007. buffer made it possible for them to work together effectively.
  1008. Eventually, the buffer grew from being an intermediary, a temporary
  1009. holding place, to being the place where work is done.  This
  1010. transformation is rather like that of a small seaport that grew into a
  1011. great city: once it was merely the place where cargo was warehoused
  1012. temporarily before being loaded onto ships; then it became a business
  1013. and cultural center in its own right.
  1014.  
  1015.    Not all buffers are associated with files.  For example, when you
  1016. start an Emacs session by typing the command `emacs' alone, without
  1017. naming any files, Emacs will start with the `*scratch*' buffer on the
  1018. screen.  This buffer is not visiting any file.  Similarly, a `*Help*'
  1019. buffer is not associated with any file.
  1020.  
  1021.    If you switch to the `*scratch*' buffer, type `(buffer-name)',
  1022. position the cursor after it, and type `C-x C-e' to evaluate the
  1023. expression, the name `"*scratch*"' is returned and will appear in the
  1024. echo area.  `"*scratch*"' is the name of the buffer.  However, if you
  1025. type `(buffer-file-name)' in the `*scratch*' buffer and evaluate that,
  1026. `nil' will appear in the echo area.  `nil' is from the Latin word for
  1027. `nothing'; in this case, it means that the `*scratch*' buffer is not
  1028. associated with any file.  (In Lisp, `nil' is also used to mean `false'
  1029. and is a synonym for the empty list, `()'.)
  1030.  
  1031.    Incidentally, if you are in the `*scratch*' buffer and want the
  1032. value returned by an expression to appear in the `*scratch*' buffer
  1033. itself rather than in the echo area, type `C-u C-x C-e' instead of `C-x
  1034. C-e'.  This causes the value returned to appear after the expression.
  1035. The buffer will look like this:
  1036.  
  1037.      (buffer-name)"*scratch*"
  1038.  
  1039. You cannot do this in Info since Info is read-only and it will not allow
  1040. you to change the contents of the buffer.  But you can do this in any
  1041. buffer you can edit; and when you write code or documentation (such as
  1042. this book), this feature is very useful.
  1043.  
  1044. 
  1045. File: emacs-lisp-intro.info,  Node: Getting Buffers,  Next: Switching Buffers,  Prev: Buffer Names,  Up: Practicing Evaluation
  1046.  
  1047. Getting Buffers
  1048. ===============
  1049.  
  1050.    The `buffer-name' function returns the _name_ of the buffer; to get
  1051. the buffer _itself_, a different function is needed: the
  1052. `current-buffer' function.  If you use this function in code, what you
  1053. get is the buffer itself.
  1054.  
  1055.    A name and the object or entity to which the name refers are
  1056. different from each other.  You are not your name.  You are a person to
  1057. whom others refer by name.  If you ask to speak to George and someone
  1058. hands you a card with the letters `G', `e', `o', `r', `g', and `e'
  1059. written on it, you might be amused, but you would not be satisfied.
  1060. You do not want to speak to the name, but to the person to whom the
  1061. name refers.  A buffer is similar: the name of the scratch buffer is
  1062. `*scratch*', but the name is not the buffer.  To get a buffer itself,
  1063. you need to use a function such as `current-buffer'.
  1064.  
  1065.    However, there is a slight complication: if you evaluate
  1066. `current-buffer' in an expression on its own, as we will do here, what
  1067. you see is a printed representation of the name of the buffer without
  1068. the contents of the buffer.  Emacs works this way for two reasons: the
  1069. buffer may be thousands of lines long--too long to be conveniently
  1070. displayed; and, another buffer may have the same contents but a
  1071. different name, and it is important to distinguish between them.
  1072.  
  1073.    Here is an expression containing the function:
  1074.  
  1075.      (current-buffer)
  1076.  
  1077. If you evaluate the expression in the usual way, `#<buffer *info*>'
  1078. appears in the echo area.  The special format indicates that the buffer
  1079. itself is being returned, rather than just its name.
  1080.  
  1081.    Incidentally, while you can type a number or symbol into a program,
  1082. you cannot do that with the printed representation of a buffer: the
  1083. only way to get a buffer itself is with a function such as
  1084. `current-buffer'.
  1085.  
  1086.    A related function is `other-buffer'.  This returns the most
  1087. recently selected buffer other than the one you are in currently.  If
  1088. you have recently switched back and forth from the `*scratch*' buffer,
  1089. `other-buffer' will return that buffer.
  1090.  
  1091.    You can see this by evaluating the expression:
  1092.  
  1093.      (other-buffer)
  1094.  
  1095. You should see `#<buffer *scratch*>' appear in the echo area, or the
  1096. name of whatever other buffer you switched back from most recently(1).
  1097.  
  1098.    ---------- Footnotes ----------
  1099.  
  1100.    (1) Actually, by default, if the buffer from which you just switched
  1101. is visible to you in another window, `other-buffer' will choose the
  1102. most recent buffer that you cannot see; this is a subtlety that I often
  1103. forget.
  1104.  
  1105.